package com.tracy.life.discovery.goods.fragment;


import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.tracy.life.R;
import com.tracy.life.bean.GoodsBean;
import com.tracy.life.constant.UrlString;
import com.tracy.life.discovery.goods.adapter.GoodsRecyclerAdapter;
import com.tracy.life.discovery.goods.presenter.GoodsPresenter;
import com.tracy.life.discovery.goods.view.GoodsView;

import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 */
public class GoodsFragment extends Fragment implements GoodsView {

    private Activity mActivity;
    private RecyclerView recyclerView;
    private GoodsPresenter mPresenter;
    private GoodsRecyclerAdapter mAdapter;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mActivity = getActivity();
        mPresenter = new GoodsPresenter(this);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_goods, container, false);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        recyclerView = (RecyclerView) view.findViewById(R.id.recyclerView_goods);
        recyclerView.setLayoutManager(new LinearLayoutManager(mActivity));
        mAdapter = new GoodsRecyclerAdapter(mActivity);
        recyclerView.setAdapter(mAdapter);
        mPresenter.onLoadDatas(UrlString.URL_GOODS);
    }

    @Override
    public void onFailure() {
        Toast.makeText(mActivity, "数据加载失败", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void loadDatas(List<GoodsBean.DataBean.CategoriesBean> datas) {
        mAdapter.setDatas(datas);
        Log.i("zjs","loadsuccess");
        mAdapter.setOnKeyWordViewItemClickListencer(new GoodsRecyclerAdapter.OnKeyWordViewItemClickListencer() {
            @Override
            public void onItemClickd(String url, View view) {
                Toast.makeText(mActivity, url, Toast.LENGTH_SHORT).show();
            }
        });
    }
}
